package com.wanglibao.woodtrade.woodtrade.ui.activity.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.entity.GoodsListEntity;
import com.wanglibao.woodtrade.woodtrade.ui.activity.MyCommodityTwoActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ItemLvMyCommodityActivityAdapter extends BaseAdapter {

    private List<GoodsListEntity.ListBean> mList = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;

    public ItemLvMyCommodityActivityAdapter(Context context, List<GoodsListEntity.ListBean> list) {
        this.context = context;
        mList = list;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void setList(List<GoodsListEntity.ListBean> list) {
        mList = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public GoodsListEntity.ListBean getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_lv_my_commodity_activity, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((GoodsListEntity.ListBean) getItem(position), (ViewHolder) convertView.getTag(), position);
        return convertView;
    }

    private void initializeViews(final GoodsListEntity.ListBean object, ViewHolder holder, final int position) {
        holder.tvItemLvMyCommodityClass.setText(object.getCategory_name());
        holder.tvItemLvMyCommodityNumber.setText(object.getQuantity() + object.getUnit_name());
        holder.tvItemLvMyCommodityShangjia.setText(object.getUp_quantity() + object.getUnit_name());
        holder.tvItemLvMyCommodityOption.setText(object.getPosition());
        holder.llItemLvMyCommodityOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MyCommodityTwoActivity.class);
                intent.putExtra("pinlei", object.getCategory_name());
                intent.putExtra("kucunliang", object.getQuantity()
                        + object.getUnit_name());
                intent.putExtra("yishangjia",object.getUp_quantity()
                        + object.getUnit_name());
                intent.putExtra("cangwei", object.getPosition());
                intent.putExtra("category_id",object.getCategory());
                intent.putExtra("Goods_id",object.getGoods_id());
                intent.putExtra("dopt",object.getDepot());
                context.startActivity(intent);
            }
        });
    }

    static class ViewHolder {
        @Bind(R.id.tv_item_lv_my_commodity_class)
        TextView tvItemLvMyCommodityClass;
        @Bind(R.id.tv_item_lv_my_commodity_number)
        TextView tvItemLvMyCommodityNumber;
        @Bind(R.id.tv_item_lv_my_commodity_shangjia)
        TextView tvItemLvMyCommodityShangjia;
        @Bind(R.id.tv_item_lv_my_commodity_option)
        TextView tvItemLvMyCommodityOption;
        @Bind(R.id.ll_item_lv_my_commodity_class)
        LinearLayout llItemLvMyCommodityOption;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
